/**
 * 07-08-商品-商品管理 <br/>
 *
 * @author fsl fzh 20151125.
 */
$(function() {

    bindEvent();
    //初始化供应信息列表
    supplyInfo(1);
    //初始化求购信息列表
    buyerInfo(1);
});
/**
 * 在页面初始化时, 绑定事件.<br/>
 *
 * @author fzh 20151126
 */
function bindEvent() {
    // 供应信息 和 求购信息 标签的切换
    $(".am-btn-group>button").click(function() {
        $(".am-btn-group>button").attr("class", "am-btn am-btn-default am-btn-sm");
        $(this).attr("class", "am-btn am-btn-primary am-btn-sm");
        var getName = $(this).attr("id");
        $(".dn").attr("style", "display:none");
        $("." + getName + "").attr("style", "display:block");
    });
    //供应信息的修改
    updateAll();
    //求购信息的修改
    updateAllBuyer();
    //获取地区
    district();
    $("#province").on("change", function() {
        var code = $(this).val();
        $("#city").show();
        city(code);
    });
    $("#city").on("change", function() {
        var code = $(this).val();
        $("#county").show();
        county(code);
    });
    //初始化时间插件
    $(".sel-order-time").datepicker();
    //搜索
    $("#search-btn").click(function() {
        var getTab = $(".am-btn-primary").attr("code");
        if (getTab == "0") {
            supplyInfo(1);
        } else {
            buyerInfo(1);
        }

    });
};
/**
 * 获取一级地区数据.  .<br/>
 * @author fsl-FZH-20151125.
 */
function district() {
    var districtTemp = _.template($("#districtTemp").html());
    $.ajax({
        url: PAGE_DATA.REST_API.addrProvince,
        type: "get",
        dataType: "json",
        success: function(result) {
            var datas = $.extend([], result.data);
            $("#province").html(districtTemp(datas));

        },
        error: function() {
            $("#province").html([]);
        }
    });
}

/**
 * 获取二级地区数据.  .<br/>
 *@param districtId {String}一级地区ID
 * @author fsl-FZH-20151125.
 */
function city(districtId) {
    var districtTemp = _.template($("#districtTemp").html());
    $.ajax({
        url: PAGE_DATA.REST_API.addrCity,
        type: "post",
        dataType: "json",
        data: {
            code: districtId,
            _token: COMM_DATA.csrfToken
        },
        success: function(result) {
            var datas = $.extend([], result.data);
            $("#city").html(districtTemp(datas));

        },
        error: function() {
            $("#city").html([]);
        }
    });
}
/**
 * 获取三级地区数据.  .<br/>
 *@param cityId {String}2级地区ID
 * @author fsl-FZH-20151125.
 */
function county(cityId) {
    var districtTemp = _.template($("#districtTemp").html());
    $.ajax({
        url: PAGE_DATA.REST_API.addrCounty,
        type: "post",
        dataType: "json",
        data: {
            code: cityId,
            _token: COMM_DATA.csrfToken
        },
        success: function(result) {
            var datas = $.extend([], result.data);
            $("#county").html(districtTemp(datas));
        },
        error: function() {
            $("#county").html([]);
        }
    });
}
/**
 * 获取供应信息列表数据..<br/>
 * @param page {String} 当前页
 * @author fsl-FZH-20151126.
 */
function supplyInfo(page) {
    var onClickObj = {
        onClick: function() {
            var clickPage = $(this).attr("data-page");
            supplyInfo(clickPage);
        }
    };
    var goodsListTemp = _.template($("#supplyListTemp").html());
    var getuserName = $("#username").val() || '';
    var gettime1 = $("#time1").val() || '';
    var gettime2 = $("#time2").val() || '';
    var getProvince = $("#province").val() || '';
    var getCity = $("#city").val() || '';
    var getCounty = $("#county").val() || '';
    $.ajax({
        url: PAGE_DATA.REST_API.get_supplyInfo_list_url,
        type: "get",
        dataType: "json",
        data: {
            username: getuserName,
            star_time: gettime1,
            end_time: gettime2,
            county: getCounty,
            city: getCity,
            province: getProvince,
            page: parseInt(page || 0),
            pageSize: 10,
        },
        success: function(result) {
            var datas = $.extend([], result.data);
            $("#supplyInfo-context").html(goodsListTemp(datas));
            // 翻页.
            var defaultPagingObj = {
                total: 0,
                pageSize: 0,
                page: 0
            };

            var pagingConf = $.extend(defaultPagingObj, result.pagingConfData, onClickObj);

            adminUtil.initPaging('#comm-paging-0', pagingConf);


        },

        error: function(result) {
            $("#supplyInfo-context").html([]);
        }
    });
}
/**
 * 获取采购信息列表数据..<br/>
 * @param page {String} 当前页
 * @author fsl-FZH-20151126.
 */
function buyerInfo(page) {
    var onClickObj = {
        onClick: function() {
            var clickPage = $(this).attr("data-page");
            buyerInfo(clickPage);
        }
    };
    var goodsListTemp = _.template($("#supplyListTemp").html());
    var getuserName = $("#username").val() || '';
    var gettime1 = $("#time1").val() || '';
    var gettime2 = $("#time2").val() || '';
    var getProvince = $("#province").val() || '';
    var getCity = $("#city").val() || '';
    var getCounty = $("#county").val() || '';
    $.ajax({
        url: PAGE_DATA.REST_API.get_buyerInfo_list_url,
        type: "get",
        dataType: "json",
        data: {
            username: getuserName,
            star_time: gettime1,
            end_time: gettime2,
            county: getCounty,
            city: getCity,
            province: getProvince,
            page: parseInt(page || 0),
            pageSize: 10,
        },
        success: function(result) {
            var datas = $.extend([], result.data);
            $("#buyerInfo-context").html(goodsListTemp(datas));
            // 翻页.
            var defaultPagingObj = {
                total: 0,
                pageSize: 0,
                page: 0
            };

            var pagingConf = $.extend(defaultPagingObj, result.pagingConfData, onClickObj);

            adminUtil.initPaging('#comm-paging-1', pagingConf);

        },

        error: function(result) {
            $("#buyerInfo-context").html([]);
        }
    });
}

/**
 * 屏蔽 恢复 所有供应信息. 单条 或 多条 都可.<br/>
 * @author fsl-FZH-20151125.
 */
function updateAll() {
    var tblBody = $("#supplyInfo-context");
    var selAllChk = $("#sel-all-chk");
    //每行数据的选中效果
    tblBody.on("click", ".sel-chk", function() {
        var chkObj = $(this).find(":checkbox");
        var chkVal = chkObj.prop("checked");
        chkObj.prop("checked", !chkVal);

        var unChk = tblBody.find(":checkbox:not(:checked)").length;
        selAllChk.prop("checked", (unChk == 0));
    });
    // 默认冒泡事件.
    tblBody.on("click", ":checkbox", function(event) {
        // event.stopPropagation();
        var chkVal = $(this).prop("checked");
        $(this).prop("checked", !chkVal);

    });
    // 修改单条数据
    tblBody.on("click", ".recovery-btn", function() {
        var state = $(this).attr("name");
        var that = this;
        var delSingle = function() {
            var updateHref = $(this).attr("href") || "";
            // var updateId = $(this).attr("code");
            updateState(updateHref, '', state);
        };

        if (state == "0") {
            var conf = {
                title: "来自网页的消息",
                message: " 确定屏蔽吗?",
                ok_func: delSingle,
                ok_context: that,
                ok_param: null
            };
            adminUtil.showConfDialog(conf);
        } else {
            var conf = {
                title: "来自网页的消息",
                message: " 确定恢复吗?",
                ok_func: delSingle,
                ok_context: that,
                ok_param: null
            };
            adminUtil.showConfDialog(conf);
        }
    });
    // 全选
    selAllChk.on("click", function() {
        var chkAllVal = $(this).prop("checked");
        tblBody.find(":checkbox").prop("checked", chkAllVal);

    });
    $(".update-all-btn").on("click", function() {

        var that = this;

        var delIdAry = [];
        tblBody.find(":checked").each(function() {
            delIdAry.push($(this).attr("id"));
        });
        if (delIdAry.length == 0) {

            var conf = {
                title: "提示",
                message: "请选择操作的数据!",
                ok_txt: "确定"
            };
            adminUtil.showMsg(conf);

            return false;
        }
        var getCode = $(this).attr("code");
        //1 = 屏蔽 0 = 恢复
        if (getCode == "1") {
            var updateMulti = function() {
                var delIds = delIdAry;
                var updateAllUrl = PAGE_DATA.REST_API.update_state_url + "?page=" +
                    $("#comm-paging-0").attr("current-page") + "&pageSize=10";
                updateState(updateAllUrl, delIds, getCode);
            };

            var conf = {
                title: "来自网页的消息",
                message: "确定屏蔽吗?",
                ok_func: updateMulti,
                ok_context: that,
                ok_param: null
            };
            adminUtil.showConfDialog(conf);
        } else {
            var updateMulti = function() {
                var delIds = delIdAry;
                var updateAllUrl = PAGE_DATA.REST_API.update_state_url + "?page=" +
                    $("#comm-paging-0").attr("current-page") + "&pageSize=10";
                updateState(updateAllUrl, delIds, getCode);
            };

            var conf = {
                title: "来自网页的消息",
                message: "确定恢复吗?",
                ok_func: updateMulti,
                ok_context: that,
                ok_param: null
            };
            adminUtil.showConfDialog(conf);
        }



    });

}
/**
 * 屏蔽 恢复 所有求购信息. 单条 或 多条 都可.<br/>
 * @author fsl-FZH-20151125.
 */
function updateAllBuyer() {
    var tblBody = $("#buyerInfo-context");
    var selAllChk = $("#sel-all-chk1");
    tblBody.on("click", ".sel-chk", function() {
        var chkObj = $(this).find(":checkbox");
        var chkVal = chkObj.prop("checked");
        chkObj.prop("checked", !chkVal);

        var unChk = tblBody.find(":checkbox:not(:checked)").length;
        selAllChk.prop("checked", (unChk == 0));
    });
    // 默认冒泡事件.
    tblBody.on("click", ":checkbox", function(event) {
        // event.stopPropagation();
        var chkVal = $(this).prop("checked");
        $(this).prop("checked", !chkVal);

    });
    // 修改单条数据
    tblBody.on("click", ".recovery-btn", function() {
        var state = $(this).attr("name");
        var that = this;
        var delSingle = function() {
            var updateHref = $(this).attr("href") || "";
            var updateId = $(this).attr("code");
            updateState(updateHref, updateId, state);
        };

        if (state == "0") {
            var conf = {
                title: "来自网页的消息",
                message: " 确定屏蔽吗?",
                ok_func: delSingle,
                ok_context: that,
                ok_param: null
            };
            adminUtil.showConfDialog(conf);
        } else {
            var conf = {
                title: "来自网页的消息",
                message: " 确定恢复吗?",
                ok_func: delSingle,
                ok_context: that,
                ok_param: null
            };
            adminUtil.showConfDialog(conf);
        }
    });
    // 全选
    selAllChk.on("click", function() {
        var chkAllVal = $(this).prop("checked");
        tblBody.find(":checkbox").prop("checked", chkAllVal);

    });
    $(".update-buyer-btn").on("click", function() {

        var that = this;

        var delIdAry = [];
        tblBody.find(":checked").each(function() {
            delIdAry.push($(this).attr("id"));
        });
        if (delIdAry.length == 0) {

            var conf = {
                title: "提示",
                message: "请选择操作的数据!",
                ok_txt: "确定"
            };
            adminUtil.showMsg(conf);

            return false;
        }
        var getCode = $(this).attr("code");
        if (getCode == "1") {
            var updateMulti = function() {
                var delIds = delIdAry;
                var updateAllUrl = PAGE_DATA.REST_API.buyerupdate_state_url + "?page=" +
                    $("#comm-paging-0").attr("current-page") + "&pageSize=10";
                updateState(updateAllUrl, delIds, getCode);
            };

            var conf = {
                title: "来自网页的消息",
                message: "确定屏蔽吗?",
                ok_func: updateMulti,
                ok_context: that,
                ok_param: null
            };
            adminUtil.showConfDialog(conf);
        } else {
            var updateMulti = function() {
                var delIds = delIdAry;
                var updateAllUrl = PAGE_DATA.REST_API.buyerupdate_state_url + "?page=" +
                    $("#comm-paging-0").attr("current-page") + "&pageSize=10";
                updateState(updateAllUrl, delIds, getCode);
            };

            var conf = {
                title: "来自网页的消息",
                message: "确定恢复吗?",
                ok_func: updateMulti,
                ok_context: that,
                ok_param: null
            };
            adminUtil.showConfDialog(conf);
        }



    });

}
/**
 * 删除商品数据. 单条 或 多条 都可.<br/>
 *
 * @param restApi {string} REST API
 * @param goodsId {array} goodsId
 *
 * @author fsl-FZH-20151125.
 */
function updateState(restApi, goodsId, code) {
    $.ajax({
        url: restApi || '',
        type: "put",
        data: {
            code: code || '',
            ids: goodsId || '',
            _token: COMM_DATA.csrfToken
        },
        success: function(result) {
            // 重新渲染列表
            var getTab = $(".am-btn-primary").attr("code");
            var getPage = result;
            var getPage1 = result;
            if (getTab == "0") {
                supplyInfo(getPage.pagingConfData.page);
            } else {
                buyerInfo(getPage1.pagingConfData.page);
            }
        },
        error: function(result) {
            // Object {readyState: 4, responseText: "{"data":"\u5bc6\u7801\u9519\u8bef"}", 
            // responseJSON: Object, status: 403, statusText: "Forbidden"}

            //var errorMsg = result.responseJSON.data || '';
            var errorMsg = '';
            var conf = {
                title: "提示",
                message: errorMsg || "获取数据失败!",
                ok_txt: "确定"
            };

            adminUtil.showMsg(conf);
        }

    });

};
